package com.zhao.fishingguideserver.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 阿里云OSS配置类
 */
@Slf4j
@Configuration
@ConfigurationProperties(prefix = "aliyun.oss")
@Data
public class OssConfig {
    
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;
    private String domain;
    
    /**
     * 创建OSS客户端
     * 仅在配置了accessKeyId时才创建
     */
    @Bean
    @ConditionalOnProperty(name = "aliyun.oss.access-key-id", matchIfMissing = false)
    public OSS ossClient() {
        // 检查必要配置是否为空
        if (accessKeyId == null || accessKeyId.trim().isEmpty()) {
            log.warn("阿里云OSS未配置，跳过初始化");
            return null;
        }
        log.info("初始化阿里云OSS客户端，endpoint={}", endpoint);
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }
}
